import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:xiongan_app_yy/pages/common/adapter/TimePicker.dart';
import 'package:xiongan_app_yy/widgets/notification/time_notification.dart';

class TimeCardWidget extends StatefulWidget {
  String hintText;
  final String title;
  final EdgeInsets margin;
  Border border;
  final int index;
  final GlobalKey scaffoldKey;
  final bool custom;
  TimeCardWidget(
      {this.hintText,
      @required this.title,
      this.margin,
      this.border,
      this.index,
      this.scaffoldKey,
      this.custom = false});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TimeCardWidgetState();
  }
}

class TimeCardWidgetState extends State<TimeCardWidget> {
  List<Widget> wList;
  String inputText = "";
  int index = 0;
  bool singleOnTap = false;

  @override
  Widget build(BuildContext context) {
    if (widget.border == null) {
      widget.border =
          Border(bottom: BorderSide(width: 1, color: Color(0xffefefef)));
    }
    wList = [
      Text(widget.hintText,
          style: TextStyle(fontSize: 16, color: Colors.black54)),
      Text(inputText, style: TextStyle(fontSize: 16)),
    ];
    return GestureDetector(
      child: Container(
        width: double.infinity,
        margin: widget.margin,
        padding: EdgeInsets.only(left: 16, right: 16),
        decoration: BoxDecoration(color: Colors.white, border: widget.border),
        child: Row(
          children: <Widget>[
            Container(
              child: Text(widget.title, style: TextStyle(fontSize: 16)),
              padding: EdgeInsets.only(top: 15, bottom: 15, right: 8),
            ),
            Container(
                height: 42,
                alignment: Alignment.centerLeft,
                child: wList[index])
          ],
        ),
      ),
      onTap: () {
/*        var now = new DateTime.now();
        print(now.year);
        DatePicker.showDatePicker(context,
            showTitleActions: true,
            currentTime: now,
            minTime: DateTime(now.year, now.month, now.day + 3),
            onChanged: (date) {
          print('change $date in time zone ' +
              date.timeZoneOffset.inHours.toString());
        }, onConfirm: (date) {
          print(date.toString().substring(0, 19));
          String s = date.toString().substring(0, 19);
          String exhibition = date.toString().substring(0, 10);
          TimeNotification(s,index: widget.index).dispatch(context);
          setState(() {
            inputText = exhibition;
            index = 1;
          });
          print('confirm $date');
        }, locale: LocaleType.zh);*/
        _showDateTime(context);

      },
    );
  }

  void _showDateTime(BuildContext context) {
    if (widget.custom) {
      var timePicker = new TimePickerAdapter(
        type: PickerDateTimeType.kYMD_AP_HM,
        isNumberMonth: true,
        strAMPM: const ["9点后", "14点后"],
        yearSuffix: "年",
        monthSuffix: "月",
        daySuffix: "日",
        value: DateTime(DateTime.now().year, DateTime.now().month,
            DateTime.now().day + 3, DateTime.now().hour),
        minValue: DateTime(DateTime.now().year, DateTime.now().month,
            DateTime.now().day),
        // twoDigitYear: true,
      );
      new Picker(
              adapter: timePicker,
              title: new Text("时间选择"),
              cancelText: "取消",
              cancelTextStyle: TextStyle(color: Color(0x8A000000)),
              confirmText: "确认",
              confirmTextStyle: TextStyle(color: Colors.blue),
              textAlign: TextAlign.right,
              selectedTextStyle: TextStyle(color: Colors.blue),
              /* delimiter: [
          PickerDelimiter(
              column: 5,
              child: Container(
                width: 16.0,
                alignment: Alignment.center,
                child: Text('至', style: TextStyle(fontWeight: FontWeight.bold)),
                color: Colors.white,
              ))
        ],*/
              onCancel: () {

              },

              onConfirm: (Picker picker, List value) {
                String exhibition;
                if (value[3] == 0) {
                  exhibition = picker.adapter.text.toString().substring(0, 11) +
                      "09:00:00.000";
                  setState(() {
                    inputText = exhibition.substring(0, 10) + " 09点后";
                    index = 1;
                  });
                  print(exhibition);
                } else {
                  exhibition = picker.adapter.text.toString().substring(0, 11) +
                      "14:00:00.000";
                  setState(() {
                    inputText = exhibition.substring(0, 10) + " 14点后";
                    index = 1;
                  });
                  print(exhibition);
                }
                TimeNotification(exhibition, index: widget.index)
                    .dispatch(context);

              },
              onSelect: (Picker picker, int index, List<int> selecteds) {
                /* if (index == 3) {
            if (selecteds[index] == 0) {
              picker.adapter.doSelect(index + 1, 8);
              picker.adapter.doSelect(index + 2, 12);
              picker.show(widget.scaffoldKey.currentState);
            } else {
              picker.adapter.doSelect(index + 1, 0);
              picker.adapter.doSelect(index + 2, 6);
              picker.show(widget.scaffoldKey.currentState);
            }
          }*/
              },)
          .showModal(this.context);
    } else {
      var timePicker = new TimePickerAdapter(
        type: PickerDateTimeType.kYMDHM,
        isNumberMonth: true,
        yearSuffix: "年",
        monthSuffix: "月",
        daySuffix: "日",
        value: DateTime(DateTime.now().year, DateTime.now().month,
            DateTime.now().day, DateTime.now().hour, DateTime.now().minute),
        minValue: DateTime(DateTime.now().year, DateTime.now().month,
            DateTime.now().day, DateTime.now().hour, DateTime.now().minute),
        // twoDigitYear: true,
      );
      new Picker(
              adapter: timePicker,
              title: new Text("时间选择"),
              cancelText: "取消",
              cancelTextStyle: TextStyle(color: Color(0x8A000000)),
              confirmText: "确认",
              confirmTextStyle: TextStyle(color: Colors.blue),
              textAlign: TextAlign.right,
              selectedTextStyle: TextStyle(color: Colors.blue),
              /* delimiter: [
          PickerDelimiter(
              column: 5,
              child: Container(
                width: 16.0,
                alignment: Alignment.center,
                child: Text('至', style: TextStyle(fontWeight: FontWeight.bold)),
                color: Colors.white,
              ))
        ],*/onCancel: () {
        setState(() {
          singleOnTap = false;
        });
      },
              onConfirm: (Picker picker, List value) {
                String exhibition = picker.adapter.text;
                setState(() {
                  inputText = exhibition.substring(0, 16);
                  index = 1;
                });
                TimeNotification(exhibition, index: widget.index)
                    .dispatch(context);
                setState(() {
                  singleOnTap = false;
                });
              },
              onSelect: (Picker picker, int index, List<int> selecteds) {
                /* if (index == 3) {
            if (selecteds[index] == 0) {
              picker.adapter.doSelect(index + 1, 8);
              picker.adapter.doSelect(index + 2, 12);
              picker.show(widget.scaffoldKey.currentState);
            } else {
              picker.adapter.doSelect(index + 1, 0);
              picker.adapter.doSelect(index + 2, 6);
              picker.show(widget.scaffoldKey.currentState);
            }
          }*/
              })
          .show(widget.scaffoldKey.currentState);
    }
  }
}
